<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Pagination Class
 * File: pagination_class.php
 * Purpose: Generating pagination
 * Todo: Add more methods
 *
 */

class pagination extends LanguageCoreLite {
	private $numResults;		// Total number of results
	private $pageSize = 10;		// Number of results to show per page
	private	$currPage;			// Current Page
	private	$areaID;		    // Current Topic
	private $areaPageID;        // What file is this?
	private $areaPageIDString;  // Actual data string
	
	/**
	* Initial class construction
	*/	
	function __construct(){
		// Do we want to show a different amount of posts per page?
		/******* change this for a more global ability *******/
		if(GlobalSettings::getSetting('posts_per_page')) {
			$this->pageSize = GlobalSettings::getSetting('posts_per_page');
		}

		// add the language file
		LanguageCoreLite::__construct(); // temporary fix
		self::add_file('pagination');
	}
	
	/**
	* Used to set pagination information
	*
	* @param string $page
	* @param string $total
	* @param string $areaid
	* @param string $pgd
	* @return void
	* @access public
	*/	
	public function setvar($page, $total, $areaid, $pgd = NULL) {
		$this->currPage = $page;
		$this->numResults = $total;
		$this->areaID = $areaid;
		$this->areaPageID = $pgd;
		
		// what string are we using?
		switch($pgd) {
			case 1:
				// is a forum
				$this->areaPageIDString = 'forum_id';
				break;
			case 3:
				// is a topic
				$this->areaPageIDString = 'topic_id';
				break;
			default:
				// not one of ours
				break;
		}
	}	
	
	/**
	* Used to calculate total pages
	*
	* @return int
	* @access public
	*/	
	public function calcPages(){
		// divide the total results by how many we need per page
		$pages = ceil($this->numResults / (float) $this->pageSize);
		
		// return the numeric value
		return $pages;
	}
	
	/**
	* Returns current page results
	*
	* @return string
	* @access public
	*/	
	public function getLimit() {
		// some basic page value checks
		if($this->currPage < 1) {
			$this->currPage = 1;
		} elseif($this->currPage > $this->calcPages()) {
			$this->currPage = 1;
		}
		
		// return the current page results for the query
		return (($this->currPage -1) * $this->pageSize) . ',' . $this->pageSize;
	}
	
	/**
	* Generate it fully
	*
	* @return string
	* @access public
	*/	
	public function displayPagination() {
		// limit to 5 and 10
		$limit_5 = $this->calcPages() - 4;
		$limit_10 = $this->calcPages() - 10;
		
		// continue output
		$output .= '<strong>' . self::get_var('PAGE_GOTO', 3) . ': </strong>';
		
		// is the current page less than 1?
		if($this->currPage < 1) {
			// set default to 1
			$this->currPage = 1;
		} elseif($this->currPage > $this->calcPages()) {
			// is the current page higher than the max calc pages?
			$this->currPage = 1;
		}
		
		// is more than likely a real number
		if($this->currPage != 1) {
			// get the previous page
			$previous = $this->currPage - 1;
			
			// output the begining
			$output .= ' [<a title="' . self::get_var('SKIP_TO_FIRST_P', 3) . '" href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => '1')) . '" class="pagination_h">|&laquo;</a>] ';
			
			// is the current page more than 10?
			if($this->currPage > 10) {
				$last_10 = $this->currPage - 10;
				$output .= ' [<a title="' . self::get_var('SKIP_B_10_P', 3) . '" href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $last_10)) . '" class="pagination_h">&laquo;</a>] ';
			}
			
			// link to previous page
			$output .= ' <a href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $previous)) . '"  class="pagination_h">' . self::get_var('PREV', 3) . '</a> ';
		}
		
		// loop through the main numbers
		for($i = 1; $i < $this->calcPages() + 1; $i++) { 
			
			// work out how close current page is, to loop number
			$within = $this->currPage-$i;
			
			if(($i == 6) && ($this->currPage == 1)) {
				$output .= ' ..... ';
			}
			
			if(($i <= 5) || ($i >= $limit_5)) {
				if($i == $this->currPage) {
					// is current value
					$output .= '<strong>' . $i . '</strong> ';
				} else {
					// isn't current value, display as a link
					$output .= '<a href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $i)) . '" class="pagination_h">' . $i . '</a> ';
				}
			} elseif($within <= 4 && $within >= -4) {
				// there's a difference in numbers, show dots
				if($within == 4) {
					$output .= ' ..... ';
				}
				
				// are we at the current page?
				if($i == $this->currPage) {
					$output .= '<strong>' . $i . '</strong> ';
				} else {
					// not the current page, hyperlink it
					$output .= '<a href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $i)) . '" class="pagination_h">' . $i . '</a> ';
				}
				
				// out of bounds again, show dots
				if($within == -4) {
					$output .= ' ..... ';
				}
			}
			
			// the limit, show some dots
			if(($i == $limit_5 - 1) && ($this->currPage == $this->calcPages())) {
				$output .= ' ..... ';
			}
		}
		
		// make sure the current page isn't the same amount as the calculate pages
		if($this->currPage != $this->calcPages()) {
			$next = $this->currPage + 1;
			$output.= ' <a href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $next)) . '" class="pagination_h">' . self::get_var('NEXT', 3) . '</a> ';
			
			// is the current page less than the 10 link limit?
			if($this->currPage < $limit_10) {
				$next_10 = $this->currPage + 10;
				$output.= ' [<a title="' . self::get_var('SKIP_10_P', 3) . '" href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $next_10)) . '" class="pagination_h">&raquo;</a>] ';
			}
			
			// allow the skipping to the last page
			$output.= ' [<a title="' . self::get_var('SKIP_TO_LAST_P', 3) . '" href="' . urlSystem::viewURL($this->areaPageID, array($this->areaPageIDString => $this->areaID, 'page_id' => $this->calcPages())) . '" class="pagination_h">&raquo;|</a>] ';
		}
		
		// output all of this
		return $output;	
	}
	
}

?>